package com.zzj.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzj.base.Result;
import com.zzj.entity.Archive;
import com.zzj.entity.Hospital;
import com.zzj.model.dto.archive.*;
import com.zzj.service.IArchiveService;
import com.zzj.utils.JWTUtil;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 健康档案 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-04-07
 */
@RestController
@RequestMapping("/archive")
public class ArchiveController {

    @Resource
    private IArchiveService archiveService;

    @Transactional
    @PostMapping("create")
    public Result create(@RequestBody @Validated ArchiveCreateDTO in){
        Archive archive = JSONObject.parseObject(JSON.toJSONString(in), Archive.class);
        archiveService.save(archive);
        return Result.success("建档成功");
    }
    @Transactional
    @PostMapping("update")
    public Result update(@RequestBody @Validated ArchiveUpdateDTO in){
        Archive archive = JSONObject.parseObject(JSON.toJSONString(in), Archive.class);
        archiveService.updateById(archive);
        return Result.success("修改成功");
    }
    @Transactional
    @PostMapping("removeById")
    public Result removeById(@RequestParam(value = "id") int id){
        archiveService.removeById(id);
        return Result.success("删除成功");
    }
    @Transactional
    @PostMapping("id")
    public Result id(@RequestParam(value = "id")int id){

        Archive archive = archiveService.getOne(Wrappers.<Archive>lambdaQuery()
                        .eq(Archive::getId, id),
                false);
        if(archive==null){
            return Result.error("不存在");
        }
        return Result.success(archive);
    }
    @Transactional
    @PostMapping("list")
    public Result list(@RequestHeader(value = "Authorization")String token){
        String username = JWTUtil.getUsername(token);
        List<Archive> list = archiveService.lambdaQuery()
                .eq(Archive::getUsername, username)
                .list();
        return Result.success(list);
    }
    @Transactional
    @PostMapping("setHeightWeight")
    public Result setHeightWeight(@RequestBody @Validated ArchiveHeightWeightDTO in){
        Archive archive = JSONObject.parseObject(JSON.toJSONString(in), Archive.class);
         archiveService.updateById(archive);
        return Result.success("记录成功");
    }
    @Transactional
    @PostMapping("setBloodPressure")
    public Result setBloodPressure(@RequestBody @Validated ArchiveBloodPressureDTO in){
        Archive archive = JSONObject.parseObject(JSON.toJSONString(in), Archive.class);
         archiveService.updateById(archive);
        return Result.success("记录成功");
    }
    @Transactional
    @PostMapping("setHealth")
    public Result setHealth(@RequestBody @Validated ArchiveHealthDTO in){
        Archive archive = JSONObject.parseObject(JSON.toJSONString(in), Archive.class);
        archiveService.updateById(archive);
        return Result.success("记录成功");
    }
}
